﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;

namespace 信息记录系统
{
    public partial class login : Form
    {
        public login()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            textBox2.PasswordChar = '*';
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim().Length == 0)
            {
                MessageBox.Show("用户名不能为空！");
                return;
            }
            if (textBox1.Text.Trim().Length == 0)
            {
                MessageBox.Show("密码不能为空！");
                return;
            }
            string constr = ConfigurationManager.ConnectionStrings["dbConnStr"].ConnectionString;
            try
            {
                MySqlConnection conn = null;
                conn = new MySqlConnection(constr);
                conn.Open();
                //登陆的时候 来sys_admin表里面查询 username password 这两个值
                string sql = string.Format("select * from sys_admin where username='{0}' and password='{1}'",
                    textBox1.Text.Trim(),
                    textBox2.Text.Trim());
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                MySqlDataReader sdr = cmd.ExecuteReader();//执行上方查询的语句
                if (sdr.Read())
                {
                    MessageBox.Show("登陆成功！");
                    Form1 f1 = new Form1();
                    f1.Show();
                    this.Hide();
                    // return;
                }
                else
                {
                    MessageBox.Show("用户名或密码错误！");
                    return;
                }
                conn.Close();//关闭数据库
            }
            catch(Exception ex)
            {
                MessageBox.Show("错误提示：" + "\r\n" + ex);
            }
        }

        private void login_Load(object sender, EventArgs e)
        {

        }
    }
}
